package com.qd.p2p.web.action.user;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.UserService;
import com.qdlc.p2p.biz.service.user.ResetPasswordStepModel;
import com.qdlc.p2p.biz.service.user.UserPasswordService;
import com.qdlc.p2p.biz.util.ValidCodeSender;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.constant.NoticeTplNid;
import com.qdlc.p2p.common.core.ReturnResult;
import com.qdlc.p2p.dal.dto.User;

/**
 * 用户密码
 * @author linya 2016-02-18
 */
public class UserPasswordAction extends BaseAction<User> implements ModelDriven<User>{

	@Resource
	UserPasswordService userPasswordService;
	
	@Resource
	UserService userService;
	
    /**
     * 修改登录密码
     */
    @Action("/member/security/modifyPwd")
    public void modifyPwd() throws Exception {
        User user = getSessionUser();
        String oldPassword = paramString("pwd");
        String newPassword = paramString("newPwd");
        String confirmNewPassword = paramString("confirmNewPwd");
        ReturnResult result = userPasswordService.modifyPassword(user, oldPassword, newPassword, confirmNewPassword);
        printWebResult(result.getErrorInfo(), result.isSuccess());
    }
    
    
    /**
     * [密码找回]第一步:跳转到密码找回页面(手机找回、邮箱找回)
     * 这里分为密码找回页面和密码重置页面，在密码找回页面中设置手机号或邮箱，通过发送验证码，来进行下一步处理，
     * 如果验证码正确，则跳转到密码重置页面。
     */
    @Action("/user/getpwd")
    public String getPwd() throws Exception {
    	
    	//如果请求有showType参数，则判断是哪种找回方式
        int showType = paramInt("showType");
        if (showType == ResetPasswordStepModel.RESET_PASSWORD_STRATEGY_TYPE_PHONE) {
        	return "getpwd_phone1";
        }else if(showType == ResetPasswordStepModel.RESET_PASSWORD_STRATEGY_TYPE_EMAIL){
        	return "getpwd_email1";
        }
        
        String nextStepPage = resetStep();
        return nextStepPage;
    }
    
    private String resetStep() throws Exception{
    	
        //如果请求有getType，则判断以那种方式重置密码
        int strategyType = paramInt("getType");
        int step = paramInt("step");
        String keyWorlds = "";
        if(strategyType == ResetPasswordStepModel.RESET_PASSWORD_STRATEGY_TYPE_PHONE){
        	keyWorlds = paramString("mobilePhone");
        }else if(strategyType == ResetPasswordStepModel.RESET_PASSWORD_STRATEGY_TYPE_EMAIL){
        	keyWorlds = paramString("email");
        }
        
        //获取重置密码步骤数据模型
        ResetPasswordStepModel stepModel = new ResetPasswordStepModel(step, keyWorlds, strategyType);
        stepModel.setRequest(request);
        
        //收集每个步骤的参数
        if(step == ResetPasswordStepModel.STEP_1){
        	String validCode = paramString("valicode");
        	stepModel.setValidCode(validCode);
        }
        if(step == ResetPasswordStepModel.STEP_2){
        	String code = paramString("code");
        	stepModel.setCode(code);
        }
        if(step == ResetPasswordStepModel.STEP_3){
        	String newPassword = paramString("pwd");
        	String confirmNewPassword = paramString("confirmNewPwd");
        	stepModel.setNewPassword(newPassword);
        	stepModel.setConfirmNewPassword(confirmNewPassword);
        }    	
        
        //执行请求步骤
        ReturnResult result = userPasswordService.resetPasswordByMobilePhone(stepModel);
        if(result.isError()){
        	printWebResult(result.getErrorInfo(), false);
        	return null;
        }else{
        	if(step == ResetPasswordStepModel.STEP_2){
        		return "getpwd_reset";
        	}else{
        		printWebResult(result.getErrorInfo(), true, result.getExtra());
        		return null;
        	}
        }
    }
    
    /**
     * [密码找回][手机找回]第二步:手机验证
     */
    @Action("/user/getpwd_phone2")
    public String getpwdPhone2() throws Exception {
    	String mobilePhone = paramString("mobilePhone");
    	User user = userService.getUserByMobilePhone(mobilePhone);
    	request.setAttribute("userInfo", user);
        return "getpwd_phone2";
    }
    
    /**
     * [密码找回][邮箱找回]第二步:邮箱验证
     */
    @Action("/user/getpwd_email2")
    public String getpwdEmail2() throws Exception {
        User user = userService.getUserByEmail(model.getEmail());
        request.setAttribute("user", user); 
        return "getpwd_email2";
    }
    

    /**
     * [找回密码]第二步:重新获取验证码
     */
    @Action("/user/getPwdCode")
    public void getPwdCode() throws Exception {
    	String email = paramString("email");
    	String mobilePhone = paramString("mobilePhone");
    	if(!StringUtils.isBlank(email)){
    		User user = userService.getUserByEmail(email);
    		ValidCodeSender.newInstance().sendCodeByEmail(user, email, NoticeTplNid.NOTICE_GET_PWD_EMAIL);
    	}else if(!StringUtils.isBlank(mobilePhone)){
    		User user = userService.getUserByMobilePhone(mobilePhone);
    		ValidCodeSender.newInstance().sendCodeByPhone(user, mobilePhone, NoticeTplNid.NOTICE_GET_PWD_PHONE);
    	}
        printWebSuccess();
    }
    
    
}
